<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script>
        class MyPromsie {
            constructor(fn) {
                this.state = "pending"
                this.value = '' // 接收promsie执行后传递回来的数据
                this.success = [] // 用来收集请求成功的函数
                this.failed = [] // 用来收集请求失败的函数
                var resolve = (value) => {
                    // console.log(this);
                    if(this.state === 'pending') {
                        this.state = 'fullfilled'
                        this.value = value
                        // console.log(this.state);
                        this.success.forEach(item => {
                            item(this.value)
                        })
                    }
                    
                }
                var reject = (err) => {
                    if(this.state === 'pending') {
                        this.state = 'rejected'
                        this.value = err
                        this.failed.forEach(item => {
                            item(this.value)
                        })
                    }
                }

                try {
                    fn(resolve, reject)
                } catch (error) {
                    console.log(error);
                }
            }

            then(success, filed) {
                // console.log(this.state);
                // console.log(this.success);
                this.success.push(success)
                this.failed.push(filed)
            }

            catch(filed) {
                this.failed.push(filed)
            }

            all() {}
        }


        var promsie = new MyPromsie(function(resolve, reject) {
            resolve(123)
            
        })


        promsie.then(res => {
            console.log(res);
        }, err => {
            console.log(err);
        })

    </script>
</body>
</html>